﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VswService_V7.Utility;

namespace VswService_V7.DataBase.Tables.L2INTERFACE
{
    public class L2INTF_BATCH
    {
        #region field
        public static readonly string tableName = "L2INTF_BATCH";
        public static readonly string fd_eventNo = "EVENT_NO";
        public static readonly string fd_taskNo = "TASK_NO";
        public static readonly string fd_stripNo = "STRIP_NO";
        public static readonly string fd_seq = "SEQ";
        public static readonly string fd_batchNoE = "BATCH_NO_E";
        public static readonly string fd_batchNoO = "BATCH_NO_O";
        public static readonly string fd_updateTime = "UPDATE_TIME";
        #endregion

        /// <summary>
        /// inser L2L3 interface batch
        /// </summary>
        /// <param name="eventNo"></param>
        /// <param name="taskNo"></param>
        /// <param name="stripNo"></param>
        /// <param name="seq"></param>
        public static void InsertIntfBatch(long eventNo, string taskNo, string stripNo, string seq)
        {
            try
            {
                string sqlINTFBATCH = string.Format("insert into {0} values({1},'{2}','{3}','{4}','{5}','{6}')",
                    tableName, eventNo, taskNo, stripNo, seq, "0", "0", DateTime.Now.ToString(Formats.GetDateFormat()));
                string sqlINTFEVENT = string.Format("insert into {0} values({1},'{2}','{3}','{4}','{5}','{6}')",
                    L2INTF_EVENT.tableName, eventNo, L2INTF_EVENT.requestBatchNo, DateTime.Now.ToString(Formats.GetDateFormat()), "1", "L1", "");
                List<string> sqlList = new List<string>();
                sqlList.Add(sqlINTFBATCH);
                sqlList.Add(sqlINTFEVENT);
                OracleHelper.ExecuteSqlTran(sqlList);
                LogHelper.WriteLog("InsertIntfBatch Success");
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("InsertIntfBatch", ex);
                throw ex;
            }
        }


        /// <summary>
		/// inser L2L3 interface batch
		/// </summary>
		/// <param name="eventNo"></param>
		/// <param name="taskNo"></param>
		/// <param name="stripNo"></param>
		/// <param name="seq"></param>
		public static void InsertIntfBatch(long eventNo, List<string> taskNo, List<string> stripNo, List<string> seq)
        {
            try
            {
                List<string> sqlList = new List<string>();
                for (int i = 0; i < stripNo.Count; i++)
                {
                    string sqlINTFBATCH = string.Format("insert into {0} values({1},'{2}','{3}','{4}','{5}','{6}','{7}')",
                    tableName, eventNo, taskNo[i], stripNo[i], seq[i], "0", "0", DateTime.Now.ToString(Formats.GetDateFormat()));
                    sqlList.Add(sqlINTFBATCH);
                }


                if(sqlList.Count>0)
                {
                    string sqlINTFEVENT = string.Format("insert into {0} values({1},'{2}','{3}','{4}','{5}','{6}')",
                    L2INTF_EVENT.tableName, eventNo, L2INTF_EVENT.requestBatchNo, DateTime.Now.ToString(Formats.GetDateFormat()), "1", "L1", "");


                    sqlList.Add(sqlINTFEVENT);
                    OracleHelper.ExecuteSqlTran(sqlList);
                    LogHelper.WriteLog("InsertIntfBatch Success");
                }
                
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("InsertIntfBatch", ex);
                throw ex;
            }
        }



        /// <summary>
        /// L2 requests batchNo from L3 : event 2011
        /// </summary>
        /// <param name="list"></param>
        public static int RequestBatchNo(DataTable stripTable)
        {
            try
            {
                if (stripTable != null && stripTable.Rows.Count > 0)
                {
                    long eventNo = OracleHelper.GetNextEventNo();
                    List<string> taskNo = new List<string>(); //stripTable.Rows[0][fd_taskNo].ToString();
                    List<string> stripNo = new List<string>(); //stripTable.Rows[0][fd_stripNo].ToString();
                    List<string> seq = new List<string>(); //(i + 1).ToString();
                    string status = "STATUS";
                    for (int i = 0; i < stripTable.Rows.Count; i++)
                    {
                        int sequence = i + 1;


                        if (stripTable.Rows[i][fd_stripNo] != null && stripTable.Rows[i][fd_stripNo].ToString() != "-1" && stripTable.Rows[i][status].ToString() == "0")
                        {
                            taskNo.Add(stripTable.Rows[i][fd_taskNo].ToString());
                            stripNo.Add(stripTable.Rows[i][fd_stripNo].ToString());
                            seq.Add(sequence.ToString());
                        }
                    }
                    if(seq.Count>0)
                    {
                        InsertIntfBatch(eventNo, taskNo, stripNo, seq);
                    }
                    return seq.Count;
                }
                else
                {
                    return 0;
                }

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("RequestBatchNo", ex);
                throw ex;
            }
        }

        /// <summary>
        /// L2 requests batchNo from L3 : event 2011
        /// </summary>
        /// <param name="list"></param>
        public static void RequestBatchNoV2(DataTable stripTable)
        {
            try
            {
                if (stripTable != null && stripTable.Rows.Count > 0)
                {
                    long eventNo = OracleHelper.GetNextEventNo();
                    List<string> taskNo = new List<string>(); //stripTable.Rows[0][fd_taskNo].ToString();
                    List<string> stripNo = new List<string>(); //stripTable.Rows[0][fd_stripNo].ToString();
                    List<string> seq = new List<string>(); //(i + 1).ToString();
                    string status = "STATUS";
                    for (int i = 0; i < stripTable.Rows.Count; i++)
                    {
                        int sequence = i + 1;


                        if (!string.IsNullOrEmpty(stripTable.Rows[i][fd_stripNo].ToString()) && stripTable.Rows[i][fd_stripNo].ToString().Length  > 5 &&
                            (stripTable.Rows[i][status].ToString()=="0" || (stripTable.Rows[i][status].ToString() == "1" &&
                            (string.IsNullOrEmpty(stripTable.Rows[i][fd_batchNoO].ToString()) || stripTable.Rows[i][fd_batchNoO].ToString().Length <= 5 ||
                            string.IsNullOrEmpty(stripTable.Rows[i][fd_batchNoE].ToString()) || stripTable.Rows[i][fd_batchNoE].ToString().Length <= 5))))
                        {
                            taskNo.Add(stripTable.Rows[i][fd_taskNo].ToString());
                            stripNo.Add(stripTable.Rows[i][fd_stripNo].ToString());
                            seq.Add(sequence.ToString());
                        }
                    }
                    if (seq.Count > 0)
                    {
                        InsertIntfBatch(eventNo, taskNo, stripNo, seq);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("RequestBatchNo", ex);
                throw ex;
            }
        }

        /// <summary>
		/// response l3 interface batch ask failed
		/// </summary>
		/// <param name="eventNo"></param>
		public static void ResponseIntfBatchFaild(int eventNo)
        {
            try
            {
                string sqlINTFBATCH = string.Format("select * from {0} where {1}={2}",
                    tableName, fd_eventNo, eventNo);
                DataTable batchTable = OracleHelper.Exct_QueryTable(sqlINTFBATCH);
                List<string> sqlList = new List<string>();
                if (batchTable != null && batchTable.Rows.Count > 0)
                {
                    for (int i = 0; i < batchTable.Rows.Count; i++)
                    {
                        if (batchTable.Rows[i][fd_stripNo] != null && batchTable.Rows[i][fd_stripNo].ToString() != "")
                        {
                            string stripNo = batchTable.Rows[i][fd_stripNo].ToString();

                            string sqlINTFEVENT = string.Format("UPDATE {0} SET {1}='{2}' where {3}='{4}'",
                               STRIP_INFO.tableName, STRIP_INFO.fd_status, STRIP_INFO.created, STRIP_INFO.fd_stripNo, stripNo
                              );
                            sqlList.Add(sqlINTFEVENT);
                        }

                    }
                   
                }
                //close the event
                string sqlCloseEvent = string.Format("update {0} set {1}='{2}' where {3}={4}",
                L2INTF_EVENT.tableName, L2INTF_EVENT.fd_eventStatus, -2, L2INTF_EVENT.fd_eventNo, eventNo);
                sqlList.Add(sqlCloseEvent);
                OracleHelper.ExecuteSqlTran(sqlList);
                LogHelper.WriteLog("ResponseIntfBatch Success");
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("ResponseIntfBatch", ex);
                throw ex;
            }
        }

    }
}
